package com.yml.mall.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yml.mall.enums.ResponseEnum;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.validation.BindingResult;

import java.util.Objects;

/**
 * @ClassName ResponsVO<T>
 * @Author Administrator
 * @Date 2020/4/7   10:04
 */
@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL) //去除为空发的属性
public class ResponseVO<T> {
    Integer status;
    String msg;
    T data;

    public ResponseVO(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ResponseVO(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ResponseVO(Integer status, T data) {
        this.status = status;
        this.data = data;
    }

    public static <T> ResponseVO<T> success(){
        return new ResponseVO<>(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
    }
    public  static <T> ResponseVO<T> successNoData(String msg){
        return new ResponseVO<>(ResponseEnum.SUCCESS.getCode(),msg);
    }
    public  static <T> ResponseVO<T> success(T data){
        return new ResponseVO<>(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc(),data);
    }
    public  static <T> ResponseVO<T> error(Integer code,String msg){
        return new ResponseVO<>(code,msg);
    }
    public  static <T> ResponseVO<T> error(ResponseEnum responseEnum){
        return new ResponseVO<>(responseEnum.getCode(),responseEnum.getDesc());
    }
    public  static <T> ResponseVO<T> error(ResponseEnum responseEnum,String msg){
        return new ResponseVO<>(responseEnum.getCode(),msg);
    }
    public  static <T> ResponseVO<T> error(ResponseEnum responseEnum, BindingResult bindingResult){
        return new ResponseVO<>(responseEnum.getCode(), Objects.requireNonNull(bindingResult.getFieldError().getField()+
                " : "+bindingResult.getFieldError().getDefaultMessage()));
    }
}
